package com.black.service.controller;

import com.black.protocol.Response;
import com.black.service.service.ServerService;
import com.black.utils.token.TokenUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
@RequestMapping("/service/server")
public class ServerController {
    final ServerService serverService;

    public ServerController(ServerService serverService) {
        this.serverService = serverService;
    }

    @GetMapping("/info")
    public Response<?> info(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || Arrays.stream(cookies).noneMatch(cookie -> cookie.getName().equals("black_ovo"))) {
            Cookie cookie = new Cookie("black_ovo", TokenUtil.makeToken("cookie"));
            cookie.setPath("/");
            cookie.setMaxAge(1800);
            cookie.setHttpOnly(true);
            cookie.setSecure(true);
            response.addCookie(cookie);
        }
        return serverService.info();
    }

    @GetMapping("/menu_list")
    public Response<?> menuList() {
        return serverService.menuList();
    }
}
